﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.Reflection;

namespace SampleControl
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
    public class ServerControl1 : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }


        /// <summary>
        /// 查询名称
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(false)]
        public string QueryName
        {
            get
            {
                String s = (String)ViewState["QueryName"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }

            set
            {
                ViewState["QueryName"] = value;
            }
        
        }











        protected override void RenderContents(HtmlTextWriter output)
        {
            string valueKey = "MyState.UserID";
            Type type = this.Page.GetType().BaseType;
            string[] valueKeys = valueKey.Split('.');
            var property = type.GetProperty("MyState");
            //object obj = new TypeConverter().ConvertTo(this.Page, type);
            if (property != null)
            {
                var value = property.GetValue(this.Page, null);

                Type mystatetype = value.GetType();
                var members = mystatetype.GetMember(valueKeys[1]);
                foreach (var member in members)
                {
                    if (member.MemberType == System.Reflection.MemberTypes.Field)
                    {
                        output.Write((member as FieldInfo).GetValue(value).ToString());
                    }
                }
            }
            output.Write(Text);
        }
    }
}
